<?php
# header of each page
function head($title='', $meta_description='', $meta_keywords='', $active='') {
header("Content-type: text/html; charset=UTF-8");
?>
<!DOCTYPE html>
<html>
<!--[if lt IE 7]>      <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]>         <html class="no-js lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]>         <html class="no-js lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js"> <!--<![endif]-->
<head>
<title><?=$title?></title>
<meta charset="utf-8">
<base href="<?=HTML_BASE?>">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="description" content="<?=$meta_description?>">

<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">        
<link href='http://fonts.googleapis.com/css?family=Ubuntu&subset=latin,cyrillic' rel='stylesheet' type='text/css'>
<link rel="stylesheet" href="css/normalize.min.css">
<link href="//maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css" rel="stylesheet">
<!--[if IE 9]> 
    <script src="js/modernizr.custom-ido.js"></script>
<![endif]-->
<!--[if lt IE 9]> 
    <script src="js/modernizr.custom-ido.js"></script>
<![endif]-->
<link rel="stylesheet" href="css/main.css">
<link rel="stylesheet" href="js/light-gallery/css/lightGallery.css">
<link rel="stylesheet" href="css/jquery.bxslider.css">
<!--[if IE 7]> 
    <link rel="stylesheet" href="css/ie7.css">
<![endif]-->
</head>

<body>
<div id="wrapper">
    <div id="left-col">
        <ul>
            <li><a href="<?=HTML_BASE.$_SESSION['lang']?>" class="fa fa-home ie-home-icon"></a></li>
            <li><a href="<?=HTML_BASE.$_SESSION['lang']?>/contacts" class="fa fa-envelope-o ie-contacts-icon"></a></li>
            
            <li><a href="https://www.facebook.com/idoadvertising" class="fa fa-facebook ie-fb-icon" target="_blank"></a></li>
            <li><a href="https://www.youtube.com/channel/UCowWcYx65726rL7M6GvSf6w" class="fa fa-youtube ie-youtube-icon" target="_blank"></a></li>
            <li><a href="http://www.pinterest.com/idoadvertisin/" class="fa fa-pinterest ie-pinterest-icon" target="_blank"></a></li>
            
            <?php 
			if ($_SESSION['lang'] == 'bg') {
			?>
            	<li><a title="English" href="<?=HTML_BASE?>en">EN</a></li>
            <?php 
			} elseif ($_SESSION['lang'] == 'en') {
			?>
                <li><a title="Български" href="<?=HTML_BASE?>bg">БГ</a></li>
            <?php 
			}
			?>
            <?php /*?><li><a href="<?=HTML_BASE.$_SESSION['lang']?>" class="fa fa-question"></a></li><?php */?>
        </ul>
    </div>
    <header>
        <?php
			   
			 
			 $sql= "SELECT menuitems FROM menus WHERE menu_id = '3'";
			 $result = query($sql);
			 $row_m = mysql_fetch_object($result);
			 $arr =  json_c_decode($row_m->menuitems);
			 function create_ul($ar,$first,$active)
			 {
					$menu_id = 3;
					echo '<ul>';
					
					
					foreach ($ar as $key => $value) {
					 
					
						$item = $value['id'];
						
						$sqlm = "SELECT parent_id,title FROM  menu_description 
							  WHERE menu_id = '".$menu_id."'AND item_id = '".$item."'
							  AND lang_id = '".$_SESSION['lang_id']."'
							 ";
					
						$rowm = mysql_fetch_object(query($sqlm)); 
					
						$sql_sef = "SELECT s.sef_url, p.class FROM  sef_url AS s , pages as p 
							  WHERE s.query = '".$rowm->parent_id."' 
							  AND s.tamplate='page' 
							  AND s.lang_id = '".$_SESSION['lang_id']."'
							  AND s.query = p.page_id
							 ";
						$row_sef = mysql_fetch_object(query($sql_sef));
						
						
						if($row_sef->sef_url == 'home') $row_sef->sef_url = '';
						
						$class = 'class="'.$row_sef->class.'"';
						if(!$active) $active = $_GET['prm1'];
						if($row_sef->sef_url == $active) $class = 'class="'.$row_sef->class.' active"';
						
						$parent = '';
						if($value['children'])$parent = 'class="isparent"';
						
						$href = 'href="#"';
						if(!$value['children']) $href = 'href="'.HTML_BASE.$_SESSION['lang'].'/'.$row_sef->sef_url.'"';
						
						
						
						echo '<li class="table-display"'.$parent.'><a '.$class.' '.$href.' ><span>'.$rowm->title.'</span></a>';
						 
						if($value['children']) create_ul($value['children'],false,$active);
						 
						echo '</li>';
					}
						
						
						echo '</ul>';
					
					
			 }
			 
			 create_ul($arr,true,$active);
					
					
					?>
    </header>
    <div id="right-col">
        <ul id="lang">
            
        </ul>
    </div>	
 			
	
<?php	
}

# footer of each page
function foot($tmp='') {
	
?>
 <footer>
        <div id="logo">
            <a href="<?=HTML_BASE?>"></a>
            &copy;  <?=date('Y')?>
        </div>
        <div id="kipo">
            Design by <a href="http://ideas-lab.eu" target="_blank">Ideas Lab</a>, 
            developed by <a href="http://studiokipo.com" target="_blank">Kipo</a>
        </div>
    </footer>
    
</div>
    
<!--vendor scripts-->
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="js/jquery.fitvids.js"></script>
<script src="js/light-gallery/js/lightGallery.min.js"></script>
<script src="js/jquery.bxslider.min.js"></script>
<script src="js/jquery.sliphover.min.js"></script>
<!--app scripts-->
<script src="js/main.js"></script>

</body>
</html>
		

<?php
}

# a redirecting function that adds the session ID if cookies are disabled and stops the execution of the script
function redirect($location) {
	if (!$location) error("Missing arguments.");

	$a = parse_url($location);
	if (!isset($a['query'])) {
		$a['query'] = '';
	}
	
	$session_delimiter = ($a['query'])?'&':'?';
	header("Location: $location".addsession($session_delimiter));
	exit;
}

# adding the session_id when cookies are disabled
function addsession($symbol="?") {
	return (!isset($_COOKIE[session_name()])&&session_id())?$symbol.session_name()."=".session_id():"";
}

function escape($string,$encoding=false) {
  return mysql_real_escape_string( (($encoding)?encoding($string):$string) );
}

function encoding($string) {
  return iconv('ISO-8859-1', 'UTF-8', $string);
}

function sth($string) {
  return htmlspecialchars(stripslashes($string));
}

# checks whether an email is valid or not
function checkEmail($email) {
	return preg_match("/^[A-z0-9\.\-]+@(?:[A-z0-9\-]+\.)+[A-z0-9]{1,6}$/", $email);
}

# Cut a text to specified chracters length
function text_format($text, $length) {
	$str = '';
	$text_length = mb_strlen($text, "UTF-8");
	 
	if ($text_length < $length) {
		$str = $text;		
	} else {
		$position_end = strpos($text, ' ', $length);
		$str = substr($text, 0, ($position_end)?$position_end:$text_length);
	}
	
	return $str;
}

function text_format_blog($text, $length) {
	$str = '';
	$text_length = mb_strlen($text, "UTF-8");
	 
	if ($text_length < $length) {
		$str = $text;		
	} else {
		$position_end = strpos($text, '<hr />', $length);
		$str = substr($text, 0, ($position_end)?$position_end:$text_length);
	}
	
	return $str;
}


# general error function
function error($s="An error occured.") {
  if (!headers_sent()) {
    head("Error");
    echo "<table cellpadding=4 width=".SITE_WIDTH." align=center border=0><tr><td align=center class=title>$s</td></tr></table>";
    foot();
  }
  else
    echo $s;
  exit;
}

# debug function for dumping a variable value
function pp($var, $stop=false) {
  echo "<pre>";
  print_r($var);
  echo "</pre>";
  if ($stop) exit;
}

# Generate numbers
function generateNumber () {

  # start with a blank number
  $number = "";

  # define possible characters
  $possible = "0123456789"; 

  # pick a random character from the possible ones
  $number = substr($possible, mt_rand(0, strlen($possible)-1), 1);

  # done!
  return $number;

}



function json_c_decode($json, $assoc = true, $depth = 512, $options = 0) {
			
				$json = preg_replace("#(/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+/)|([\s\t]//.*)|(^//.*)#", '', $json);
				
				if(version_compare(phpversion(), '5.4.0', '>=')) {
					$json = json_decode($json, $assoc, $depth, $options);
				}
				elseif(version_compare(phpversion(), '5.3.0', '>=')) {
					$json = json_decode($json, $assoc, $depth);
				}
				else {
					$json = json_decode($json, $assoc);
				}
			
				return $json;
		}



?>